package com.xiong.jdbc;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;

public class C3p0DataSource {

    private ComboPooledDataSource dataSource;

    public void init() throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/spring-demo?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&zeroDateTimeBehavior=convertToNull");
        dataSource.setUser("root");
        dataSource.setPassword("123456");
        dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
        //当数据库连接池中连接不够时，一次性向服务器申请的连接数
        dataSource.setAcquireIncrement(5);
        //初始化时的连接数量
        dataSource.setInitialPoolSize(10);
        // 每个连接中最多可以使用的Statement的个数
        dataSource.setMaxStatementsPerConnection(2);
        //数据库连接池中维护的最多的Statement的个数
        dataSource.setMaxStatements(50);
        dataSource.setMaxPoolSize(20);
        dataSource.setMinPoolSize(5);
        this.dataSource=dataSource;
    }

    public Connection getConnection(){
        try {
            return this.dataSource.getConnection();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    public C3p0DataSource(){
        try {
            this.init();
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
    }
}
